package leetcode_1_200;

import node.ListNode;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class LeeCode_141 {
    public static void main(String[] args) {
        ListNode z = new ListNode(3);
        ListNode l = new ListNode(2);
        z.next = l;
        l.next = new ListNode(0);
        l.next.next = new ListNode(-4);
        l.next.next.next = l;
        System.out.println(hasCycle(z));
    }

    private static boolean hasCycle(ListNode head) {
        Set<ListNode> set = new HashSet<>();
        ListNode cur = head;
        while (cur != null){
            if (set.contains(cur)){
                return true;
            }
            set.add(cur);
            cur = cur.next;
        }
        return false;
    }


}
